using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class MyCustomModules_UploadFiles : System.Web.UI.Page
{
    string strFileNames = string.Empty;
    string conn = System.Configuration.ConfigurationManager.AppSettings.Get("MSSQLConnectionString");
    int UserID;
    Guid uguid;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RegisterStartupScript("AddFirstControl", "<script language='javascript' type='text/javascript' >addNewBrowseControl(); </script>");

            if (Request.QueryString["filenames"] != null)
            {
                strFileNames = Request.QueryString["filenames"].ToString();
                UploadAndSaveFiles();
            }

        }
    }

    protected void UploadAndSaveFiles()
    {
        foreach( string str in strFileNames.Split(';'))
        {
        
            string filename = Path.GetFileName(str);

                //fld.SaveAs(Server.MapPath("~/") + Common.GetNewFileName(UserID, filename, DateTime.Now.Year.ToString()));

            mojoPortal.Data.SqlParameterHelper sph = new mojoPortal.Data.SqlParameterHelper(conn, "usp_AddNewDocument", CommandType.StoredProcedure, 6);
            sph.DefineSqlParameter("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID);
            sph.DefineSqlParameter("@UserGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, uguid);
            sph.DefineSqlParameter("@ParentGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, uguid);
            sph.DefineSqlParameter("@Year", SqlDbType.Int, ParameterDirection.Input, DateTime.Now.Year.ToString());
            sph.DefineSqlParameter("@FilePath", SqlDbType.NVarChar, 1000, ParameterDirection.Input, Common.GetNewFileName(UserID, filename, DateTime.Now.Year.ToString()));
            sph.DefineSqlParameter("@DocType", SqlDbType.NVarChar, 50, ParameterDirection.Input,
                Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings.Get("General")));

            int result = Convert.ToInt32(sph.ExecuteNonQuery());


            if (result == -1)
            {
                ;// lblMessage.Text = "Form16 already uploaded for this year.";
            }
            else if (result > 0)
            {
                ;
                //MailMerge msg = new MailMerge(txtName.Text, "friend's name", "friend's email address",
                //    txtPAN.Text, DateTime.Now.Year.ToString(), filename, Common.GetNewFileName(UserID, filename, DateTime.Now.Year.ToString()), "DocUpload");
                //send mail to uploader
                //MailHelper.SendMailMessage(HttpContext.Current.User.Identity.Name.ToString(),
                //    "", System.Configuration.ConfigurationManager.AppSettings.Get("SMTPTo"), msg);

                //Response.Redirect(Request.Url.ToString());
            }
            else
                ; //lblMessage.Text = "Error occured";
        }
    }

    protected void btnAddMore_Click(object sender, EventArgs e)
    {
        FileUpload fl = new FileUpload();
        fl.Width = 350;
        pnlFileNames.Controls.Add(fl);
    }
    protected void btnUploadAll_Click(object sender, EventArgs e)
    {
        string str = hdnAllFileNames.Value;
    }

    protected void GetUserGUID()
    {
         // Get User Details
        System.Data.SqlClient.SqlDataReader dr = mojoPortal.Data.SqlHelper.ExecuteReader
           (conn, CommandType.StoredProcedure, "usp_GetUserDetails",
           new System.Data.SqlClient.SqlParameter("@UserEmail", HttpContext.Current.User.Identity.Name.ToString()));
        if (dr.HasRows)
        {

            dr.Read();

            uguid = new Guid(dr["UserGuid"].ToString());
            UserID = Convert.ToInt32(dr["UserID"]);
        }
        dr.Close();



    }
}
